VocaCourse={
	is_passed:false,
	count:0,
	task_info:function(){
		var list_words=function(data,grade){
			$container=$('.words_'+grade);
			$.each(data[grade],function(key,obj){
				$container.append('<div title="'+obj['d']+'">'+obj['w']+'</div>');
			});
		}
		$.post(VocaHandlerURL.course,{action:'info'},function(json){
			if(json.success){
				if(json.data[0]==0){
					$('.loading').fadeOut('fast',function(){
						$('.none').fadeIn('fast');
					});
				}else{
					var data=json.data;
					$('.course .total div').text(data[0]['total']);
					$('.course .done div').text(data[0]['done']);
					$('.course .undone div').text(data[0]['undone']);
					$('.course .familiarity_1 div').text(data[1].length);
					$('.course .familiarity_2 div').text(data[2].length);
					$('.course .familiarity_3 div').text(data[3].length);
					$('.course .familiarity_4 div').text(data[4].length);
					list_words(data,1);
					list_words(data,2);
					list_words(data,3);
					list_words(data,4);
					$('.loading').fadeOut('fast',function(){
						$('.course').fadeIn('fast');
					});
				}
			}else{
				error_dialog();
			}
		}).fail(function(){
			error_dialog();
		});
	},
	initialize:function(){
		$('#start_learning').click(function(){
			var $this=$(this);
			var $loader=$this.next();
			$this.attr('disabled','disabled')
			$loader.fadeIn();
			$.post(VocaHandlerURL.course,{action:'initialize'},function(json){
				$loader.fadeOut('fast',function(){
					$this.removeAttr('disabled');
				});
				if(json.success){
					window.location.href='/course/'+json.data.record_id+'/learning';
				}else{
					error_dialog();
				}
			}).fail(function(){
				error_dialog();
			});
		});
	},
	quiz:function(){
		get_quiz();
	}
}

function toggle_loading(show){
	if(show){
		$('#learning_loading').fadeIn('fast');
	}else{
		$('#learning_loading').css('display','none');
	}
}

function toggle_next(show){
	if(show){
		$('.go_next').fadeIn('fast');
	}else{
		$('.go_next').fadeOut('fast');
	}
}

function item_id(item_id){
	if(item_id){
		$.data(document,'item_id',item_id);
	}else{
		return $.data(document,'item_id');
	}
}
function word_id(word_id){
	if(word_id){
		$.data(document,'word_id',word_id);
	}else{
		return $.data(document,'word_id');
	}
}

function compose_quiz(data){
	VocaCourse.is_passed=false;
	toggle_next(VocaCourse.is_passed);
	item_id(data.item_id);
	word_id(data.word_id);
	$('.quiz').css('display','none');
	if(data.familiarity==1){
		quiz_1(data);
	}else if(data.familiarity==2){
		quiz_2(data);
	}else if(data.familiarity==3){
		quiz_3(data);
	}
}

function quiz_1(data){
	$spell = $('#quiz_1 #spell');
	$spell.removeAttr('readonly').val('');
	$spell.next().css('display','none');
	$('#quiz_1 .word').text(data.word);
	$('#quiz_1 .definition').text(data.definition);
	$spell.keyup(function(){
		var $this=$(this);
		if($.trim($this.val())==data.word){
			$this.attr('readonly','readonly');
			$this.next().fadeIn('fast');
			$this.get(0).blur();
			VocaCourse.is_passed=true;
			toggle_loading(VocaCourse.is_passed);
			pass();
		}
	});
	$('#quiz_1').fadeIn('fast');
}

function quiz_2(data){
	$('#quiz_2 .shuffle_wrapper ul').remove();
	$('#quiz_2 .shuffle_wrapper').append('<ul class="shuffle"></ul>');
	var $shuffle = $('#quiz_2 .shuffle_wrapper ul');
	$("#quiz_2 .word").text('');
	$shuffle.next().css('display','none');
	var word = data.word;
	$('#quiz_2 .definition').text(data.definition);
	var chars = shuffle(word.split(''));
	if(chars.join('')==word){
		chars = shuffle(word.split(''));
	}
	$.each(chars,function(i,c){
		$shuffle.append('<li><div>'+c+'</div></li>');
	});
	$shuffle.dragsort({ draggingClass:'placeHolder', overClass:'over',dropCallback: function(){
		var data = $("#quiz_2 .shuffle li").map(function() { return $(this).children().html(); }).get();
		var my_word = data.join('');
		console.info(word+'='+my_word);
		if(word == my_word){
			$("#quiz_2 .word").text(word);
			$shuffle.find('li').removeAttr('draggable');
			$shuffle.next().fadeIn();
			VocaCourse.is_passed=true;
			toggle_loading(VocaCourse.is_passed);
			pass();
		}
	}});
	$('#quiz_2').fadeIn('fast');
}

function quiz_3(data){
	$("#quiz_3 .word").text(data.word);
	var $definitions = $('#quiz_3 .definitions');
	var definitions=[data.definition];
	$.each(data.definitions,function(i,o){
		definitions[i+1]=o.definition;
	});
	definitions=shuffle(definitions);
	$.each(definitions,function(i,o){
		$definitions.append('<div><span class="number">'+(i+1)+'</span><span class="value">'+o+'</span></div>');
	});
	$definitions.find('div').mousedown(function(){
		if($(this).find('.value').text()==data.definition){
			alert('true');
		}else{
			alert('false');
		}

	});
	$('#quiz_3').fadeIn('fast');
}

function pass(callback){
	$.post(
		VocaHandlerURL.learning,
		{action:'pass',item_id:item_id(),word_id:word_id()},
		function(json){
			if(json.success){
				if($.isFunction(callback))callback();
				toggle_loading();
				toggle_next(VocaCourse.is_passed);
			}else{
				error_dialog();
			}
		}
	).fail(function(){
		error_dialog();
	});
}

function get_quiz(){
	$.post(
		VocaHandlerURL.learning,
		{action:'quiz',record_id:$('#data_record_id').val()},
		function(json){
			if(json.success){
				compose_quiz(json.data);
			}else{
				error_dialog();
			}
		}
	).fail(function(){
		error_dialog();
	});
}

function go_next(){
	if(VocaCourse.is_passed){
		$('.quiz').css('display','none');
	}
}

$(function(){
	$('.go_next').click(function(){
		get_quiz();
	});
});

function shuffle(array) {
	var currentIndex = array.length, temporaryValue, randomIndex;
	while (0 !== currentIndex) {
		randomIndex = Math.floor(Math.random() * currentIndex);
		currentIndex -= 1;
		temporaryValue = array[currentIndex];
		array[currentIndex] = array[randomIndex];
		array[randomIndex] = temporaryValue;
	}
	return array;
}